package com.lecampus.businessCampus_moments.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lecampus.businessCampus_moments.entity.Article;
import com.lecampus.businessCampus_moments.entity.ArticleComment;
import com.lecampus.businessCampus_moments.entity.ArticleImage;
import com.lecampus.businessCampus_moments.mapper.ArticleCommentMapper;
import com.lecampus.businessCampus_moments.mapper.ArticleImageMapper;
import com.lecampus.businessCampus_moments.mapper.ArticleMapper;
import com.lecampus.businessCampus_moments.mapper.UserMapper;
import com.lecampus.businessCampus_moments.service.ArticleService;
import com.lecampus.businessCampus_moments.vo.ArticleImageVO;
import com.lecampus.businessCampus_moments.vo.ArticleVO;
import com.lecampus.commons.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-04-10
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleImageMapper articleImageMapper;
    @Autowired
    private ArticleCommentMapper articleCommentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    private User user;
    private ArticleImageVO articleImageVO;
    private ArticleVO articleVO;
    private List<ArticleImage> articleImageList;
    private List<Article> articleList;
    private List<ArticleImageVO> articleImageVOList;
    private List<ArticleVO> articleVOList;

    private QueryWrapper<Article> articleQueryWrapper;
    private QueryWrapper<ArticleImage> articleImageQueryWrapper;
    private QueryWrapper<ArticleComment> articleCommentQueryWrapper;

    /**
     * 根据用户id查找文章
     */
    @Override
    public List<ArticleVO> findArticleByUserId(String userId,long currentPage,long size) {
        //分页
        Page<Article> page = new Page<>(currentPage,size);
        //条件
        articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("user_id",userId);

        articleMapper.selectPage(page,articleQueryWrapper);
        articleList = page.getRecords();

        articleVOList = articleVOListUtil(articleList);

        return articleVOList;
    }

    /**
     * 根据文章id查找文章
     */
    @Override
    public ArticleVO findArticleById(String articleId) {
        //增加浏览量
        plusBrowse(articleId);

        Article article = articleMapper.selectById(articleId);

        return articleVOUtil(article);
    }

    /**
     * 根据文章id删除文章
     */
    @Override
    public void deleteArticleById(String articleId) {
        articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("id",articleId);
        articleList = articleMapper.selectList(articleQueryWrapper);

        for (Article article : articleList) {
            //删图片
            articleImageQueryWrapper = new QueryWrapper<>();
            articleImageQueryWrapper.eq("article_id",article.getId());
            articleImageMapper.delete(articleImageQueryWrapper);
            //删评论
            articleCommentQueryWrapper = new QueryWrapper<>();
            articleCommentQueryWrapper.eq("article_id",article.getId());
            articleCommentMapper.delete(articleCommentQueryWrapper);
        }

        articleMapper.delete(articleQueryWrapper);
    }

    /**
     * 根据文章标签id查找文章
     */
    @Override
    public List<ArticleVO> findArticleByLabelId(String labelId,long currentPage,long size) {
        //分页
        Page<Article> page = new Page<>(currentPage,size);
        // labelId 为空
        if ("".equals(labelId)) {
            articleQueryWrapper = new QueryWrapper<>();
            articleMapper.selectPage(page,articleQueryWrapper);
            articleList = page.getRecords();

            articleVOList = articleVOListUtil(articleList);
        } else {
            // labelId 不为空
            articleQueryWrapper = new QueryWrapper<>();
            articleQueryWrapper.eq("label_id",labelId);
            articleMapper.selectPage(page,articleQueryWrapper);
            articleList = page.getRecords();

            articleVOList = articleVOListUtil(articleList);
        }
        return articleVOList;
    }

    /**
     * 封装文章
     */
    private ArticleVO articleVOUtil(Article article) {
        if (article != null) {
            articleImageQueryWrapper = new QueryWrapper<>();
            articleImageQueryWrapper.eq("article_id",article.getId());

            articleImageList = articleImageMapper.selectList(articleImageQueryWrapper);

            articleImageVOList = new ArrayList<>();
            for (ArticleImage articleImage : articleImageList) {
                articleImageVO = new ArticleImageVO();
                BeanUtils.copyProperties(articleImage,articleImageVO);

                articleImageVOList.add(articleImageVO);
            }

            user = userMapper.selectById(article.getUserId());

            articleVO = new ArticleVO();
            BeanUtils.copyProperties(user,articleVO);
            BeanUtils.copyProperties(article,articleVO);

            articleVO.setArticleImageVOList(articleImageVOList);
        }

        return articleVO;
    }

    /**
     * 封装文章列表
     */
    private List<ArticleVO> articleVOListUtil(List<Article> articleList) {
        articleVOList = new ArrayList<>();
        for (Article article : articleList) {
            articleVOUtil(article);
            articleVOList.add(articleVO);
        }
        return articleVOList;
    }

    /**
     * 增加浏览量
     */
    private void plusBrowse(String articleId) {
        Article article = articleMapper.selectById(articleId);
        article.setBrowse(article.getBrowse() + 1);
        articleMapper.updateById(article);
    }

    /**
     * 点赞
     */
    @Override
    public void likes(String articleId, int flag) {
        Article article = articleMapper.selectById(articleId);

        //点赞
        if (flag == 0) {
            article.setLikeSum(article.getLikeSum() + 1);
        } else if (flag == 1 && article.getLikeSum() > 0) {
            //取消点赞
            article.setLikeSum(article.getLikeSum() - 1);
        }
        articleMapper.updateById(article);
    }
}
